import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-04-08
 * Time: 12:37
 */
public class test2 {
    public boolean isValid(String ss) {
        Stack<Character> s = new Stack<>();
        int n = ss.length();
        for(int i=0;i<n;i++){
            char ch = ss.charAt(i);
            if(ch=='(' || ch=='[' || ch=='{'){
                s.push(ch);
            }else{
                if(s.isEmpty()){
                    return false;
                }
                char ch2 = s.pop();
                if( (ch!=')' && ch2=='(') || (ch!=']' && ch2=='[') || (ch!='}' && ch2=='{') ){
                    return false;
                }
            }
        }
        return s.isEmpty();
    }
}
